package Demo.Java窗口;


import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Calculator extends JFrame {
    //文本输入
    private JTextField txtInput;
    //创一个面板
    private JPanel mainPanel;

    private String op;

    private double num1,num2;
    private String result;

    private boolean flag;

   public void Calculator(){
       //窗口命名标题
       this.setTitle("计算器");
     //设置窗口位置 和大小
       this.setBounds(400,100,250,400);
       //关闭窗口
       this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       //new一个文本输入框
       txtInput = new JTextField();
       //设置文本框是否可用
       txtInput.setEnabled(false);
       //设置文本对齐方式
       txtInput.setHorizontalAlignment(JTextField.RIGHT);

       //new一个面板
       mainPanel =new JPanel();
       //将面板设置成网格布局
       mainPanel.setLayout(new GridLayout(4,4,5,5));
       //创建按钮
       mainPanel.add(getButton("7"));
       mainPanel.add(getButton("8"));
       mainPanel.add(getButton("9"));
       mainPanel.add(getButton("*"));
       mainPanel.add(getButton("4"));
       mainPanel.add(getButton("5"));
       mainPanel.add(getButton("6"));
       mainPanel.add(getButton("/"));
       mainPanel.add(getButton("1"));
       mainPanel.add(getButton("2"));
       mainPanel.add(getButton("3"));
       mainPanel.add(getButton("+"));
       mainPanel.add(getButton("0"));
       mainPanel.add(getButton("."));
       mainPanel.add(getButton("="));
       mainPanel.add(getButton("-"));
       //把文本框和面板放入窗口 并确定位置
        this.add(txtInput,BorderLayout.NORTH);
        this.add(mainPanel,BorderLayout.CENTER);

   }

    private JButton getButton(String key){

        JButton btn = new JButton(key);
        btn.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {



            }
        });

        return btn;
    }
    public static void main(String[] args) {
        Calculator calculator = new Calculator();
        calculator.setVisible(true);
    }
}

